package com.lanou3g.dllo.baidumusic.mine;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.database.MyFavorite;
import com.lanou3g.dllo.baidumusic.database.RecentlyPlay;
import com.lanou3g.dllo.baidumusic.database.SingleLiteOrm;
import com.lanou3g.dllo.baidumusic.mine.localmusic.LocalMusic;
import com.lanou3g.dllo.baidumusic.mine.localmusic.LocalMusicBean;
import com.lanou3g.dllo.baidumusic.main.MainActivity;
import com.lanou3g.dllo.baidumusic.mine.myfavorite.MyFavoriteManager;

import cn.bmob.v3.BmobUser;

/**
 * Created by dllo on 16/6/17.
 */
public class MineFragment extends BaseFragment {

    private RelativeLayout mineRecentlyPlayLayout;
    private TextView mineRecentlyPlaySongsNumber, mineMyFavoriteSongsCounts, mineLocalMusicSongs;
    private RelativeLayout mineMyFavoriteItemlayout, localMusicLayout;
    private RefreshBroadcast refreshBroadcast;


    @Override
    public int setLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView(View view) {
        mineMyFavoriteItemlayout = (RelativeLayout) view.findViewById(R.id.mine_myfavorite_itemlayout);
        mineRecentlyPlayLayout = (RelativeLayout) view.findViewById(R.id.mine_recentlyplay_layout);
        mineRecentlyPlaySongsNumber = (TextView) view.findViewById(R.id.mine_recentlyplay_songsnumber);
        mineMyFavoriteSongsCounts = (TextView) view.findViewById(R.id.mine_myfavorite_songscounts);
        mineLocalMusicSongs = (TextView) view.findViewById(R.id.mine_localmusic_songs);
        localMusicLayout = (RelativeLayout) view.findViewById(R.id.localmusic_layout);
    }

    @Override
    public void initData() {

        // 我的界面的最近播放跳转到详情界面
        mineRecentlyPlayLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).toRecentlyPlayDetail();
            }
        });

        // 我的界面的我喜欢的单曲跳转到详情界面
        mineMyFavoriteItemlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).toMyFavoriteDetail();
            }
        });

        // 我的界面的本地音乐的跳转到详情界面
        localMusicLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).toLocalMusicDetail();
            }
        });

        // 我的界面最近播放的歌曲数
        mineRecentlyPlaySongsNumber.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(RecentlyPlay.class).size() + "首歌");
        // 我的界面我喜欢的单曲的歌曲数
        BmobUser bmobUser = BmobUser.getCurrentUser(context);
        if (bmobUser == null) {
            mineMyFavoriteSongsCounts.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(MyFavorite.class).size() + "首");
        } else {
            mineMyFavoriteSongsCounts.setText("共" + MyFavoriteManager.getInstance().getMyFavorites().size() + "首");
        }
        // 我的界面本地歌曲歌曲数,先查询在设置歌曲数
        LocalMusic.query(context);
        mineLocalMusicSongs.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(LocalMusicBean.class).size() + "首");

        refreshBroadcast = new RefreshBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(context.getPackageName() + ".REFRESHMYFAVORITE");
        filter.addAction(context.getPackageName() + ".REFRESHRECRNTLYPLAY");
        filter.addAction(context.getPackageName() + ".DELETEREFRESH");
        filter.addAction(context.getPackageName() + ".DELETEREFRESHRECENTLYPLAY");
        context.registerReceiver(refreshBroadcast, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context.unregisterReceiver(refreshBroadcast);
    }

    class RefreshBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 刷新我的界面最近播放的歌曲数
            mineRecentlyPlaySongsNumber.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(RecentlyPlay.class).size() + "首歌");
            // 刷新我的界面我喜欢的单曲的歌曲数
            BmobUser bmobUser = BmobUser.getCurrentUser(context);
            if (bmobUser == null) {
                mineMyFavoriteSongsCounts.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(MyFavorite.class).size() + "首");
            } else {
                mineMyFavoriteSongsCounts.setText("共" + MyFavoriteManager.getInstance().getMyFavorites().size() + "首");
            }
            // 刷新我的界面本地歌曲歌曲数
            mineLocalMusicSongs.setText("共" + SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(LocalMusicBean.class).size() + "首");
        }
    }

}
